package net.gz01.wallet.common;

import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.geom.Line2D;
import java.awt.image.BufferedImage;
import java.io.IOException;

import javax.imageio.ImageIO;

import net.gz01.util.ZODate;
import net.gz01.wallet.web.config.SystemEnum;
import net.gz01.wallet.web.model.TradeOrder;

/** 电子回单工具 */
public class Certificate {

	private final static String FONT = "宋体";

	/** 画出电子回单图形 */
	public static BufferedImage createImage(TradeOrder order) throws IOException {
		int WIDTH = 720, HEIGHT = 420;
		BufferedImage image = new BufferedImage(WIDTH, HEIGHT, BufferedImage.TYPE_INT_RGB);
		// 获取图形上下文
		Graphics2D g = image.createGraphics();
		g.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_NEAREST_NEIGHBOR);
		// 图形抗锯齿
		g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
		// 字体抗锯齿
		g.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);

		// 设定背景色
		g.setColor(Color.WHITE);
		g.fillRect(0, 0, WIDTH, HEIGHT);
		g.setColor(Color.BLACK);
		// 大标题居中
		String title = "友利钱包电子回单";
		Font fontBig = new Font(FONT, Font.PLAIN, 22);
		g.setFont(fontBig);
		FontMetrics metrics = g.getFontMetrics(fontBig);
		g.drawString(title, (WIDTH - metrics.stringWidth(title))/2, 30);
		// 分割线：虚线
		g.setStroke(new BasicStroke(1, BasicStroke.CAP_BUTT, BasicStroke.JOIN_ROUND, 1f, new float[]{ 1f, 0f, 1f }, 1f));
		g.draw(new Line2D.Double(0, 50, WIDTH, 50));
		// 小标题
		g.setFont(new Font(FONT, Font.PLAIN, 16));
		String dzhd = order.getCreateTime().replace("-", "").replace(":", "").replace(" ", "")
				+ "-" + order.getOrderNo();

		String bizType="类型:" + order.getBizTypeShow();

		g.drawString(bizType + " 回单号："+dzhd, 0, 90);
		// 表格内容字体
		g.setFont(new Font(FONT, Font.PLAIN, 14));
		// 打印日期
		g.drawString("打印日期：" + ZODate.getDate(), WIDTH - 260, 90);
		// 表格：四个边框
		g.setStroke(new BasicStroke());
		int top = 100;
		g.draw(new Line2D.Double(0,				top, 				WIDTH, 	top));		// 上
		g.draw(new Line2D.Double(0, 				HEIGHT-1, 	WIDTH, 	HEIGHT-1)); // 下
		g.draw(new Line2D.Double(0, 				top, 				0, 				HEIGHT)); // 左
		g.draw(new Line2D.Double(WIDTH-1,	top, 				WIDTH-1, HEIGHT)); // 右

		float lh = 28;	// 行高line-height
		float ot = top + 20;	// 距离顶部便宜offset-top
		float c2 = 80;	// 第2列的起始位置(即第1列的宽度)
		/***********************out: 付款方******* in:收款方*******************/
		String OutAccountName = "", InAccountName="";
		String OutAccountNo = "", InAccountNo="";
		String OutBankName = "", InBankName="";
		/*********************************************/
		if(SystemEnum.BizType.Charge.getValue().equals(order.getBizType())){
			OutAccountName = order.getPayer();
			InAccountName = order.getAccountName();
			InAccountNo = order.getAccountNo();
			InBankName = order.getBankName()==null?"" :order.getBankName() ;
		}
		if(SystemEnum.BizType.Transfer_xx.getValue().equals(order.getBizType())){
			OutAccountName = order.getPayer();
			InAccountName = order.getAccountName();
			InAccountNo = order.getAccountNo();
			InBankName = order.getBankName()==null?"" :order.getBankName();
		}

		if(SystemEnum.BizType.TransferOut.getValue().equals(order.getBizType())||
				SystemEnum.BizType.ExpenseOut.getValue().equals(order.getBizType())||
				SystemEnum.BizType.DF_Company.getValue().equals(order.getBizType()) ||
				SystemEnum.BizType.DF_Personal.getValue().equals(order.getBizType()) ||
				SystemEnum.BizType.Withdraw_Company.getValue().equals(order.getBizType())||
				SystemEnum.BizType.Withdraw_Personal.getValue().equals(order.getBizType())){
			OutAccountName = order.getMerchName();
			OutAccountNo = order.getMerchCode();

			InAccountName = order.getAccountName();
			InAccountNo = order.getAccountNo();
			InBankName = order.getBankName()==null?"":order.getBankName();
		}

		if(SystemEnum.BizType.TransferIn.getValue().equals(order.getBizType()) ||
				SystemEnum.BizType.ExpenseIn.getValue().equals(order.getBizType())){

			InAccountName = order.getMerchName();
			InAccountNo = order.getMerchCode();

			OutAccountName = order.getAccountName();
			OutAccountNo = order.getAccountNo();
		}


		// 付款人
		g.drawString("付款人", 		10, ot + lh);
		g.drawString("全称", 			c2, ot);			g.drawString(OutAccountName, 					c2*2, ot);
		g.drawString("账户", 			c2, ot + lh);		g.drawString(OutAccountNo,			c2*2, ot + lh);
		g.drawString("开户银行", 	c2, ot + lh*2);	g.drawString(OutBankName, 		c2*2, ot + lh*2);
		// 收款人
		g.drawString("收款人", WIDTH/2+10, ot + lh);
		g.drawString("全称", 			WIDTH/2+c2, ot);				g.drawString(InAccountName, 	WIDTH/2+c2*2, ot);
		g.drawString("账户", 			WIDTH/2+c2, ot + lh);		g.drawString(InAccountNo,	WIDTH/2+c2*2, ot + lh);
		g.drawString("开户银行",	WIDTH/2+c2, ot + lh*2);		g.drawString(InBankName, WIDTH/2+c2*2, ot + lh*2);
		// 金额
		g.drawString("金额", 		10, ot + lh*3.5f);
		g.drawString("RMB：" + order.getAmountFormat(), c2, ot + lh*3);
		g.drawString("人民币(大写)：" + order.getAmountInWords(), c2, ot + lh*4);
		// 单号／记账时间
		g.drawString("交易单号", 					10,					ot + lh*5);
		g.drawString(order.getOrderNo(),		c2, 					ot + lh*5);
		g.drawString("记账时间",					WIDTH/2+10,	ot + lh*5);
		g.drawString(order.getCreateTime(), 	WIDTH/2+c2,	ot + lh*5);
		g.drawString("用户名称", 					10,					ot + lh*6);
		g.drawString(order.getMerchName(),		c2, 					ot + lh*6);
		g.drawString("用户编号",					WIDTH/2+10,	ot + lh*6);
		g.drawString(order.getMerchCode(), 	WIDTH/2+c2,	ot + lh*6);
		// 电子签章
		g.drawString("电子签章", 					10,					ot + lh*8.5f);
		// 摘要／商品名
		g.drawString("摘要", 							WIDTH/2+10, 	ot + lh*8.5f);
		String remarks = order.getGoodsName()==null?"":order.getGoodsName() + ";" + order.getPurpose() + ";";
		g.drawString(remarks, WIDTH/2 + c2, ot + lh*8.5f);

		// 画表格边框
		g.setColor(Color.GRAY);
		// 横线
		// 收款人的横线
		g.draw(new Line2D.Double(c2 - 10, ot + 9, WIDTH / 2, ot + 9));
		g.draw(new Line2D.Double(c2 - 10, ot + lh + 9, WIDTH / 2, ot + lh + 9));
		// 付款人的横线
		g.draw(new Line2D.Double(WIDTH / 2 + c2 - 10, ot + 9, WIDTH - 2, ot + 9));
		g.draw(new Line2D.Double(WIDTH / 2 + c2 - 10, ot + lh + 9, WIDTH - 2, ot + lh + 9));
		// 金额的横线
		g.draw(new Line2D.Double(c2 - 10, ot + lh*3 + 9, WIDTH - 2, ot + lh*3 + 9));
		// 完整的横线
		g.draw(new Line2D.Double(0 + 1, ot + lh*2 + 9, WIDTH - 2, ot + lh*2 + 9));
		g.draw(new Line2D.Double(0 + 1, ot + lh*4 + 9, WIDTH - 2, ot + lh*4 + 9));
		g.draw(new Line2D.Double(0 + 1, ot + lh*5 + 9, WIDTH - 2, ot + lh*5 + 9));
		g.draw(new Line2D.Double(0 + 1, ot + lh*6 + 9, WIDTH - 2, ot + lh*6 + 9));

		// 竖线
		g.draw(new Line2D.Double(c2 - 10,						top + 1, c2 - 10, HEIGHT - 1));
		g.draw(new Line2D.Double(c2 * 2 - 10,					top + 1, c2 * 2 - 10, ot + lh * 2 + 9));
		g.draw(new Line2D.Double(WIDTH / 2 + c2 * 2 - 10,	top + 1, WIDTH / 2 + c2 * 2 - 10, ot + lh * 2 + 9));
		g.draw(new Line2D.Double(WIDTH / 2,					top + 1, WIDTH / 2, ot + lh * 2 + 9));
		g.draw(new Line2D.Double(WIDTH / 2 + c2 - 10,	top + 1, WIDTH / 2 + c2 - 10, ot + lh * 2 + 9));
		g.draw(new Line2D.Double(WIDTH / 2,					ot + lh * 4 + 9, WIDTH / 2, HEIGHT - 1));
		g.draw(new Line2D.Double(WIDTH / 2 + c2 - 10,	ot + lh * 4 + 9, WIDTH / 2 + c2 - 10, HEIGHT - 1));

		// 印章
		BufferedImage img = ImageIO.read(Certificate.class.getResourceAsStream("/yinzhang.png"));
		g.drawImage(img, (int)c2*2, (int)(ot + lh*5), null);

		g.dispose();
		return image;
	}

}
